package rbac.service.array;

import rbac.domain.dto.UserDto;
import rbac.domain.entity.Dept;
import rbac.domain.entity.Role;
import rbac.domain.entity.User;
import rbac.domain.vo.Result;
import rbac.domain.vo.RoleVo;
import rbac.domain.vo.UserVo;
import rbac.service.IDeptService;
import rbac.service.IRoleService;
import rbac.service.IUserService;
import rbac.util.SingletonFactory;
import rbac.view.DeptView;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 基于数值存储的业务层-⽤户管理模块
 */

public class UserService extends BaseService<User, UserDto, UserVo> implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;

    public UserService() { // 将构造函数声明为私有
        data = new User[DATA_SIZE + 1];
        temp = new UserVo[DATA_SIZE];
        this.deptService = SingletonFactory.getInstance(DeptService.class);
        this.roleService = SingletonFactory.getInstance(RoleService.class);
    }

    @Override
    public User createEntity(UserDto dto, int id){
        LocalDateTime now = LocalDateTime.now();
        return new User(id, now, now, dto.username, dto.name, dto.mobile, dto.email,
                dto.deptId, dto.roleId); // 创建新的部⻔对象，使⽤传⼊的部⻔名称和电话
    }

    @Override
    // 将部⻔实体对象转换为部⻔视图对象
    public UserVo convertToVo(User user){
        // 查找部⻔
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }

        // 查找角色
        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }

        // 通过类名直接调⽤，⽆需创建 DeptService类的实例
        return new UserVo(user.getId(), user.createAt, user.updateAt, user.username, user.name, user.mobile, user.email,
                user.deptId, user.roleId, user.enabled, deptName, roleName);
    }

    @Override
    public void updateEntity(UserDto dto, User user) {
        user.updateAt = LocalDateTime.now();
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    // 检查是否有⽤户属于deptId对应的部⻔
    public boolean deptInUse(int deptId) {
        boolean inUse = false;
        for (User user : data) {
            // 检查⽤户是否存在且该⽤户的部门ID等于传⼊的部⻔ID
            if (user != null && user.deptId == deptId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    // 检查是否有⽤户属于roleId对应的部⻔
    public boolean roleInUse(int roleId) {
        boolean inUse = false;
        for (User user : data) {
            // 检查⽤户是否存在且该⽤户的部门ID等于传⼊的部⻔ID
            if (user != null && user.roleId == roleId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result toggle(int id) {
        if (id < 1 || id >= idCounter || data[id] == null) {
            return Result.failure("切换状态失败，不存在该数据");
        }

        // 反转`enabled状态
        data[id].enabled = !data[id].enabled;

        return Result.success(data[id].enabled ? "启⽤成功" : "禁⽤成功", null);
    }
}

